package com.delong.service.market;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.delong.dao.DaoHelper;
import com.delong.dao.market.ScheduleMapper;
import com.delong.model.market.Schedule;

public class ScheduleService {
	public void addElement(Schedule element) {
		SqlSession session = null;
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			ScheduleMapper mapper = session.getMapper(ScheduleMapper.class);
			mapper.addElement(element);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}

	}

	public List<Schedule> getListElement(int page_id, int pageno, int limit) {
		SqlSession session = null;
		List<Schedule> result = new ArrayList<Schedule>();
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			ScheduleMapper mapper = session.getMapper(ScheduleMapper.class);
			result = mapper.selectList(page_id, pageno, limit);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return result;

	}

	public int getListElementCount(int page_id) {
		SqlSession session = null;
		int result = 0;
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			ScheduleMapper mapper = session.getMapper(ScheduleMapper.class);
			result = mapper.selectListcount(page_id);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return result;
	}

	public void changeStatus(int id, int status) {
		SqlSession session = null;

		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			ScheduleMapper mapper = session.getMapper(ScheduleMapper.class);
			mapper.changeStatus(status, id);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}

	}

	public void changeElement(Schedule element) {
		SqlSession session = null;

		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			ScheduleMapper mapper = session.getMapper(ScheduleMapper.class);
			mapper.changeElement(element);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}

	}

	public Schedule getElement(int id) {
		SqlSession session = null;
		Schedule element = new Schedule();
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			ScheduleMapper mapper = session.getMapper(ScheduleMapper.class);
			element = mapper.selectElementById(id);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return element;

	}

	public boolean selectValid(int pageId) {
		SqlSession session = null;
		boolean element = false;
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			ScheduleMapper mapper = session.getMapper(ScheduleMapper.class);
			int result = mapper.selectValid(pageId);
			return result > 0;

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return element;

	}

}
